package de.playmee.views;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import de.playmee.R;
import de.playmee.handler.AccountHandler;
import de.playmee.tasks.RegisterAccountTask;
import de.playmee.tasks.RegisterAccountTask.RegisterAccountListener;

public class RegisterDialog {

	private final String TAG = "RegisterDialog";
	
	private Context context;
	private AccountHandler aHandler;
	private RegisterAccountListener listener;
	
	private EditText usernameInput;
	private EditText passwordInput;
	private Button positiveButton;
	
	public RegisterDialog(Context context, AccountHandler aHandler,
			RegisterAccountListener listener) {
		this.context = context;
		this.aHandler = aHandler;
		this.listener = listener;
	}
	
	/**
	 * creates the dialog
	 * 
	 * @return AlertDialog
	 */
	public AlertDialog create() {
		Log.i(TAG, "Creating the dialog...");
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setCancelable(false);
		builder.setIcon(R.drawable.ic_menu_invite);
		builder.setTitle("Register");
		
		LinearLayout layout = new LinearLayout(context);
		layout.setOrientation(LinearLayout.VERTICAL);
		
		usernameInput = new EditText(context);
		usernameInput.setSingleLine();
		usernameInput.setHint(R.string.login_user);
		addTextChangedListener(usernameInput);
		layout.addView(usernameInput);
		
		passwordInput = new EditText(context);
		passwordInput.setSingleLine();
		passwordInput.setHint(R.string.login_password);
		addTextChangedListener(passwordInput);
		layout.addView(passwordInput);
		
		builder.setView(layout);
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// register username and password in an asynchronous task
				String username = usernameInput.getText().toString().trim();
				String password = passwordInput.getText().toString().trim();
				(new RegisterAccountTask(aHandler, username, password, listener)).execute();
			}
		});
		
		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// nothing to do
			}
		});
		
		return builder.create();
	}
	
	/**
	 * adds a TextChangedListener to the given button that enables or disables
	 * the positive button of the dialog
	 * 
	 * @param button
	 */
	private void addTextChangedListener(EditText editText) {
		editText.addTextChangedListener(new TextWatcher() {
			@Override
			public void afterTextChanged(Editable s) {
				// TODO min password length
				if (usernameInput.getText().toString().trim().length() > 0
						&& passwordInput.getText().toString().trim().length() > 0)
					positiveButton.setEnabled(true);
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// nothing to do
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// nothing to do
			}
		});
	}
	
	/**
	 * creates the dialog and displays it on the screen
	 */
	public void show() {
		AlertDialog dialog = create();
		dialog.show();
		positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
		positiveButton.setEnabled(false);
	}
}